# -*- encoding: utf-8 -*-
"""
    Created by Rachel on 2025/03/06
"""
import unittest
from app import app
import json

class TestLogin(unittest.TestCase):
    """定义测试案例"""
    def setUp(self):
        """在执行具体的测试方法前，先被调用"""
        self.app = app
        app.config['TESTING'] = True
        self.client = app.test_client()

    def test_create_miss_param(self):
        """测试模拟场景，测试创建不传递模型名或创建数据"""
        data = {
            "model_name": "",
            "create_data": [
                {"account": "1", "password": "1234556789", "username": "张三"},
                {"account": "2", "password": "1234556789", "username": "赵三"},
                {"account": "3", "password": "1234556789", "username": "王三"}
            ]
        }
        response = self.client.post("/public/create", json=data, content_type='application/json')

        resp_dict = json.loads(response.data)

        # 验证：是否存在code字符串在字典中
        self.assertIn("code", resp_dict)

        # 验证错误码是不是 1
        code = resp_dict.get("code")
        self.assertEqual(code, 1)

        # 验证返回信息
        msg = resp_dict.get('message')
        self.assertEqual(msg, "请传入模型名称")

        data_2 = {
            "model_name": "user",
            "create_data": []
        }

        # 测试只传name
        response = self.client.post("/public/create", json=data_2, content_type='application/json')

        resp_dict = json.loads(response.data)

        # 使用断言进行验证
        self.assertIn("code", resp_dict)

        # 验证错误码 65535
        code = resp_dict.get("code")
        self.assertEqual(code, 1)

        # 验证返回信息
        msg = resp_dict.get('message')
        self.assertEqual(msg, "请传入创建数据")

        data_3 = {
            "model_name": "user",
            "create_data": [
                {"account": "7", "password": "1234556789", "username": "张三"},
                {"account": "8", "password": "1234556789", "username": "赵三"},
                {"account": "9", "password": "1234556789", "username": "王三"}
            ]
        }
        response = self.client.post("/public/create", json=data_3, content_type='application/json')

        resp_dict = json.loads(response.data)

        # 验证：是否存在code字符串在字典中
        self.assertIn("code", resp_dict)

        # 验证错误码是不是 1
        code = resp_dict.get("code")
        self.assertEqual(code, 0)


    # def test_wrong_name_password(self):
    #     """测试用户名或密码错误"""
    #     # 使用客户端向后端发送post请求, data指明发送的数据，会返回一个响应对象
    #     response = self.client.post("/login", data={"name": "admin", "password": "123456789"})
    #
    #     # respoonse.data是响应体数据
    #     resp_json = response.data
    #
    #     # 按照json解析
    #     resp_dict = json.loads(resp_json)
    #
    #     # 使用断言进行验证
    #     self.assertIn("code", resp_dict)
    #
    #     # 验证错误码
    #     code = resp_dict.get("code")
    #     self.assertEqual(code, 65535)
    #
    #     # 验证返回信息
    #     msg = resp_dict.get('message')
    #     self.assertEqual(msg, "用户名或密码错误")


if __name__ == '__main__':
    unittest.main()